﻿using System.Collections.Generic;
using Common;
using Entities;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WMS.DtoModels;

namespace Portal.Controllers
{
    public class PermissionController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public PermissionController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        public IActionResult LoadAllPermission(PermissionInfoReqDto permissionInfoReqDto)
        {
            var totalCount = 0;
            var querySqlQueryable = this._sqlSugarClient.Queryable<sys_permission>();

            querySqlQueryable = querySqlQueryable.Where(new { PId = permissionInfoReqDto.Id });
    
            var queryResult = querySqlQueryable.ToPageList(permissionInfoReqDto.Page, permissionInfoReqDto.Limit, ref totalCount);


            return Json(new
            {
                Code = 0,
                Msg = "查询成功",
                Count = totalCount,
                Data = queryResult
            });
        }

        public IActionResult LoadPermissionManagerLeftTreeJson()
        {
            var querySqlQueryable = this._sqlSugarClient.Queryable<sys_permission>().Where(x=>x.type== "menu").ToList();

            var list = new List<DTreeResDto>();

            foreach (var item in querySqlQueryable)
            {
                list.Add(new DTreeResDto
                {
                    Id = item.id,
                    CheckArr = item.open.ToString(),
                    Title = item.title,
                    ParentId = item.pid
                });
            }

            var result = new LayUiStandardResDto<DTreeResDto>
            {
                Code = 0,
                Msg = "查询成功",
                Data = list
            };

            return Json(result);
        }
    }
}